import re
import sys

def decimal_to_base36(num):
    """将十进制数转换为36进制"""
    if num < 0:
        return '-' + decimal_to_base36(-num)
    
    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    base = len(digits)
    result = ''
    
    while num > 0:
        remainder = num % base
        result = digits[remainder] + result
        num //= base
    
    return result or '0'

def compress_id_card(id_card):
    """将身份证号码压缩为36进制字符串"""
    if len(id_card) != 18 or not id_card[:17].isdigit() or not (id_card[-1].isdigit() or id_card[-1].upper() == 'X'):
        return "无效的身份证号码"
    
    # 将身份证号码转换为整数
    last_char = id_card[-1].upper()
    if last_char == 'X':
        last_char = '10'
    else:
        last_char = str(int(last_char))
    id_int = int(id_card[:17] + last_char)
    
    # 转换为36进制
    compressed_id = decimal_to_base36(id_int)
    return compressed_id

def base36_to_decimal(base36_str):
    """将36进制字符串转换为十进制整数"""
    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    base = len(digits)
    num = 0
    
    for char in base36_str:
        num = num * base + digits.index(char.upper())
    return num

def decompress_id_card(compressed_id):
    """将36进制字符串解压缩为原始身份证号码"""
    id_int = base36_to_decimal(compressed_id)
    id_str = str(id_int)
    
    # 如果最后一位是10，替换为'X'
    if id_str.endswith('10'):
        id_str = id_str[:-2] + 'X'
    return id_str[:17] + id_str[-1]

def main():
    # 检查命令行参数
    if len(sys.argv) < 2:
        print("用法: python script.py 身份证号码或36进制码")
        sys.exit(1)
    
    input_data = sys.argv[1].strip()

    # 判断输入是否为身份证号码
    if len(input_data) == 18:
        # 验证身份证号码格式
        if re.match(r'^\d{17}[\dX]$', input_data):
            compressed_id = compress_id_card(input_data)
            print(f"{compressed_id}")
        else:
            print("无效的身份证号码.")
    else:
        # 验证36进制码格式
        if re.match(r'^[0-9A-Za-z]+$', input_data):
            original_id = decompress_id_card(input_data)
            print(f"{original_id}")
        else:
            print("无效的36进制码.")

if __name__ == "__main__":
    main()